package com.ultrasoft.farmasoft.repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ultrasoft.farmasoft.domain.Stock;

@Repository(value = "stockDao")
@Transactional(readOnly = false)
public class StockDaoImpl implements StockDao {

	private EntityManager em = null;
	@PersistenceContext
	public void setEntityManager(EntityManager em) {
		this.em = em;
	} 
	public List<Stock> getListStock() {
		TypedQuery<Stock> q = em.createNamedQuery("Stock.findAll",Stock.class);
    	return q.getResultList();
	}

	public List<Stock> getListStockByPharmacyIdByUserId(Integer pharmacyId, Integer userId) {
		TypedQuery<Stock> q = em.createNamedQuery("Stock.findByPharmacyIdByUserId",Stock.class);
    	q.setParameter("pharmacyId",pharmacyId);
    	q.setParameter("userId",userId);
    	return q.getResultList();
	}

	public List<Stock> getListStockByPharmacyId(Integer pharmacyId) {
		TypedQuery<Stock> q = em.createNamedQuery("Stock.findByPharmacyId",Stock.class);
    	q.setParameter("pharmacyId",pharmacyId);
    	return q.getResultList();
	}

	public List<Stock> getListStockByUserId(Integer userId) {
		TypedQuery<Stock> q = em.createNamedQuery("Stock.findByUserId",Stock.class);
    	q.setParameter("userId",userId);
    	return q.getResultList();
	}

	public Stock findByStockId(Integer stockId) {
		Stock res=null;
		try {
			res=em.find(Stock.class,stockId);
		} catch (Exception e) {
			// TODO: handle exception
		}
    	return res;
	}

	public void update(Stock stock) {
		em.merge(stock);
	}

	public void remove(Stock stock) {
		em.remove(em.getReference(Stock.class,stock.getStockId()));
	}

	public void add(Stock stock) {
		em.persist(stock);
	}

}
